from flask import Flask, request
from flask_cors import CORS

from pyeCharts.serverForPyeCharts import serverForPyeCharts

app = Flask(__name__)
CORS(app)  # 启用CORS


# 返回指定年份的地区发展指数柱状图
@app.route("/developmentBar", methods=['POST', 'GET'])
def developmentBar():
    year = request.args.get("year")
    print('接收到请求...==>', year)
    result = serverForPyeCharts.getDataForDevelopment(year)  # 实际需要返回的结果

    result_json = result.dump_options_with_quotes()  # 生成JSON字符串
    return result_json  # 将JSON字符串包装在JSON响应中


# 返回指定年份的地区发展指数柱状图
@app.route("/futureDevelopmentBar", methods=['POST', 'GET'])
def futureDevelopmentBar():
    year = request.args.get("year")
    print('接收到请求...==>', year)
    result = serverForPyeCharts.getFutureDataForDevelopment(year)  # 实际需要返回的结果

    result_json = result.dump_options_with_quotes()  # 生成JSON字符串
    return result_json  # 将JSON字符串包装在JSON响应中


@app.route("/developmentMap", methods=['POST', 'GET'])
def developmentMap():
    year = int(request.args.get("year"))
    print('接收到请求...==>', year)
    result = serverForPyeCharts.getDevelopmentMap(year)  # 实际需要返回的结果
    result_json = result.dump_options_with_quotes()  # 生成JSON字符串
    return result_json  # 将JSON字符串包装在JSON响应中


# 根据城市名获取预测GDP折线图
@app.route("/futureGDP", methods=['POST', 'GET'])
def futureGDP():
    city_name = request.args.get("city_name")
    print('接收到请求...==>', city_name)
    result = serverForPyeCharts.drawLineForFutureGDP(city_name)  # 实际需要返回的结果
    result_json = result.dump_options_with_quotes()  # 生成JSON字符串
    return result_json


# 根据城市名和指标名获取对应折线图
@app.route("/indicator", methods=['POST', 'GET'])
def getIndicatorByCityName():
    city_name = request.args.get("city_name")
    indicator_name = request.args.get("indicator")
    print('接收到请求...==>', city_name, indicator_name)
    result = serverForPyeCharts.drawLineForCity(city_name, indicator_name)  # 实际需要返回的结果
    result_json = result.dump_options_with_quotes()  # 生成JSON字符串
    return result_json


# 获取各项指标的权重饼图
@app.route("/weight", methods=['POST', 'GET'])
def getWeightPie():
    result = serverForPyeCharts.get_indicator_weight()
    result_json = result.dump_options_with_quotes()
    return result_json


if __name__ == "__main__":
    app.run(debug=True)
